import { Component, OnInit, ChangeDetectorRef, ViewChild, Inject, forwardRef } from '@angular/core';
import { path } from '../../../../path.config';
import { masterService } from '../../../../../lib/master.service';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { PrepaidVoucherEditComponent } from './prepaid-voucher-edit/prepaid-voucher-edit.component';
import { AppComponent } from '../../../../app.component';
import { NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'app-grid-grade',
    templateUrl: './grid.component.html',
    styleUrls: ['./grid.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class GridComponent implements OnInit {
    @ViewChild('pvModal') pvModal: ModalDirective;
    @ViewChild('editForm') editForm: PrepaidVoucherEditComponent;

    // 表格数据路径
    public getList: any = path.accountRecharge.prepaidVoucher.getGradeList;
    // 目前启用、禁用按钮
    public isEnabled: any = path.accountRecharge.prepaidVoucher.editEnabled;
    // 获取充值档详细信息
    public getGradeInfo = path.accountRecharge.prepaidVoucher.getGradeInfo;
    // 检查商品编号
    public checkSu = path.accountRecharge.prepaidVoucher.checkSu;
    // 设置充值档默认选中
    public setDefault = path.accountRecharge.prepaidVoucher.setDefault;


    private data: any;
    public rows: Array<any> = [];
    // 表格数据头部标题
    public columns: Array<any> = [
        { title: '排序', name: 'sort' },
        { title: '充值档', name: 'amount' },
        { title: '前台显示礼包总额', name: 'displayAmount' },
        { title: '是否启用', name: 'btn' },
        { title: '默认选择', name: 'btn' },
        { title: '按钮', name: 'btn' }
    ];

    public editModel = {};

    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        private confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) { }

    public ngOnInit() {
        this.onChangeTable();
    }

    // 渲染表格
    public onChangeTable = () => {
        this._masterService
            .getData(this.getList)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data;
                    this.rows.forEach((val, index) => {
                        val.isDefault = '' + val.isDefault;
                    })
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    // 打开编辑弹窗
    public openEditModal(rowId) {
        let urlObject = {
            id: rowId
        }
        // 获取列表详情
        this._masterService
            .getData(this.getGradeInfo, urlObject)
            .subscribe((res) => {
                if (res.data && res.statusCode == 200) {
                    this.editForm.condition = res.data;
                    this.editForm.rows = res.data.items;
                    this.pvModal.show();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    public closeModal() {
        this.pvModal.hide();
        this.onChangeTable();
    }

    // 目前启用、禁用
    private IsEnable(rowId: string, rowEnabled) {
        this.confirmServ.confirm({
            nzTitle: '是否确认启用或禁用？',
            nzOnOk() {
                postIsEnable();
            }
        })

        let postIsEnable = () => {
            let IsEnableObj = {
                id: rowId,
                isActive: 1 - rowEnabled
            }
            this._masterService
                .postData(this.isEnabled, IsEnableObj)
                .subscribe((res) => {
                    if (res.statusCode == 200) {
                        this.onChangeTable();
                    } else if (res.statusCode == 401) {
                        this.app.logOff();
                    } else {
                        this.app.toastError('错误信息: ' + res.errorMessage);
                    }
                })
        }

    }

    private IsDefault(rowId) {
        let that = this;
        this.confirmServ.confirm({
            nzTitle: '是否确认启用或禁用？',
            nzOnOk() {
                postData();
            },
            nzOnCancel() {
                that.rows.forEach((val) => {
                    if (val.id == rowId) {
                        val.isDefault = 0;
                    }
                })
                that.cdRef.detectChanges();
            }
        })
        let postData = () => {
            let urlObject = {
                id: rowId
            }
            this._masterService
                .postData(this.setDefault, urlObject)
                .subscribe((res) => {
                    if (res.statusCode == 200) {
                        this.onChangeTable();
                    } else if (res.statusCode == 401) {
                        this.app.logOff();
                    } else {
                        this.app.toastError('错误信息： ' + res.errorMessage);
                    }
                })
        }
    }
}
